package com.dog.basics.service.sys.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.dto.admin.sys.DictAddDto;
import com.dog.basics.bean.dto.admin.sys.DictEditDto;
import com.dog.basics.bean.model.sys.Dict;
import com.dog.basics.bean.vo.admin.sys.DictInfoVo;
import com.dog.basics.bean.vo.admin.sys.DictPageVo;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.dao.sys.DictMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.sys.IDictService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 基础字典 服务实现类
 * </p>
 *
 * @author guoy
 * @since 2020-03-27
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements IDictService {
    /**
     * @Description:  分页版本列表
     * @Param:  phone
     * @return:  IPage<AppVersionPageVo>
     * @Author: wph
     * @Date: 2020/3/27 0027
     */
    @Override
    public IPage<DictPageVo> getPageList(PageParam page) {
        IPage iPage = this.page(page, new LambdaQueryWrapper<Dict>().orderByDesc(Dict::getCreateTime));
        List<Dict> records = iPage.getRecords();
        iPage.setRecords(records.stream().map(cljgAppVersionInfo -> {
            DictPageVo dictPageVo = new DictPageVo();
            BeanUtil.copyProperties(cljgAppVersionInfo, dictPageVo);
            return dictPageVo;
        }).collect(Collectors.toList()));
        return iPage;
    }


    /**
     * @Description:  增加版本
     * @Param:  appVersionAddDto
     * @return:  Boolean
     * @Author: wph
     * @Date: 2020/3/27 0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveObj(DictAddDto dictAddDto) {
        Dict dict = new Dict();
        BeanUtil.copyProperties(dictAddDto, dict);
        dict.setCreateTime(LocalDateTime.now());
        dict.setCreateUser(SecurityUtils.getSysUser().getUserId());
        return this.save(dict);
    }


    /**
     * @Description:  修改版本
     * @Param:  appVersionEditDto
     * @return:  Boolean
     * @Author: wph
     * @Date: 2020/3/27 0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editObj(DictEditDto dictEditDto) {
        Dict dict = this.getById(dictEditDto.getDictTypeId());
        BeanUtil.copyProperties(dictEditDto, dict);
        dict.setUpdateTime(LocalDateTime.now());
        dict.setUpdateUser(SecurityUtils.getSysUser().getUserId());
        return this.updateById(dict);
    }


    /**
     * @Description:  删除版本
     * @Param:  ids
     * @return:  Boolean
     * @Author: wph
     * @Date: 2020/3/27 0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteObj(List<Long> ids) {
        return this.removeByIds(ids);
    }

    /**
     * @Description:  查看版本
     * @Param:  id
     * @return:  AppVersionInfoVo
     * @Author: wph
     * @Date: 2020/3/27 0027
     */
    @Override
    public DictInfoVo info(Long id) {
        DictInfoVo dictInfoVo = new DictInfoVo();
        Dict dict = this.getById(id);
        BeanUtil.copyProperties(dict, dictInfoVo);
        return dictInfoVo;
    }
}
